package flc.ast.dialog;

import android.content.Context;
import android.view.Gravity;
import android.view.View;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.PathUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.stark.ve.EditorHolder;
import com.stark.ve.core.IEditorListener;

import flc.ast.R;
import flc.ast.databinding.DialogFormatConvBinding;
import flc.ast.utils.Content;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.base.BaseSmartDialog;
import stark.common.basic.utils.FileP2pUtil;
import stark.common.basic.utils.FileUtil;
import stark.common.basic.utils.RxUtil;

public class FormatConvDialog extends BaseSmartDialog<DialogFormatConvBinding> implements View.OnClickListener {

    public String videoPath, newName, format;
    public int width, height, ml;

    public FormatConvDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected boolean canceledOnTouchOutside() {
        return true;
    }

    @Override
    protected int getGravity() {
        return Gravity.CENTER;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_format_conv;
    }

    @Override
    protected void initView(View view) {
        comVideo();
    }

    @Override
    public void onClick(View v) {

    }

    //开始转换(帧率无效，不管它)
    private void comVideo() {

        EditorHolder.editor().videoCompress(videoPath, width, height, ml, new IEditorListener() {
            @Override
            public void onSuccess(String outFilePath) {
                saveVideo(outFilePath, format);
            }

            @Override
            public void onFailure(String errMsg) {
                ToastUtils.showShort(R.string.conv_def);
            }

            @Override
            public void onProgress(int progress) {
                mDataBinding.pbFormat.setProgress(progress);
                mDataBinding.tvProgress.setText(progress + getContext().getString(R.string.unit_percent));
                if (progress == 100) {
                    mDataBinding.tvTitle.setText(getContext().getString(R.string.format_comp_title));
                    mDataBinding.tvText.setText(getContext().getString(R.string.format_comp_tip));
                }
            }
        });
    }

    //保存视频
    private void saveVideo(String path, String format) {
        RxUtil.create(new RxUtil.Callback<String>() {
            @Override
            public void doBackground(ObservableEmitter<String> emitter) {
                String filePath = FileUtil.generateFilePath(Content.myTemp, format);
                FileUtils.copy(path, filePath);
                emitter.onNext(filePath);
            }

            @Override
            public void accept(String savePath) {
                //重命名
                FileUtils.rename(savePath, newName + format);
                String newPath = PathUtils.getFilesPathExternalFirst() + Content.myTemp + "/" + newName + format;
                //保存到相册
                FileP2pUtil.copyPrivateVideoToPublic(getContext(), newPath);
            }
        });
    }

    private Ilistener listener;

    public void setListener(Ilistener listener) {
        this.listener = listener;
    }

    public interface Ilistener {

    }
}
